package com.friends.library.persistence;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateClass {

	
  public  int diff(Date date1, Date date2) {
    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();

    c1.setTime(date1);
    c2.setTime(date2);
    int diffDay = 0;

    if (c1.before(c2)) {
      diffDay = countDiffDay(c1, c2);
    } else {
      diffDay = countDiffDay(c2, c1);
    }

    return diffDay;
  }

  public  int DateDiff(Date date1, Date date2) {
	  int fine=3;
    int diffDay = diff(date1, date2);
    if(diffDay<7){
    	
    	diffDay=0;
    	return diffDay;
    }else{
    	diffDay=(diffDay-7)*fine;
    	return diffDay;
    	/*if(diffDay>300){
    		System.out.println("newBook");
    	}else{
    	System.out.println(diffDay);
    	}*/
    	
    }
    //System.out.println("Different Day : " + diffDay);
  }

  public  int countDiffDay(Calendar c1, Calendar c2) {
    int returnInt = 0;
    while (!c1.after(c2)) {
      c1.add(Calendar.DAY_OF_MONTH, 1);
      returnInt++;
    }

    if (returnInt > 0) {
      returnInt = returnInt - 1;
    }

    return (returnInt);
  }

  public  Date makeDate(String dateString) throws Exception {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
    return formatter.parse(dateString);
  }

  public  int dynamic(String s1,String s2)throws Exception{

    java.util.Date d1 = makeDate("2012-04-06");
    java.util.Date d2 = makeDate("2012-03-02");
 return    DateDiff(d1, d2);

  }

}